package singleList;

public class Test02 {
    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode headA,
                                                      MySingleList.ListNode headB) {
        //定义一个虚拟节点 ， 此时涉及到获取静态内部类对象
        MySingleList.ListNode newH = new MySingleList.ListNode(-1);
        MySingleList.ListNode temp = newH;
        while(headA != null && headB != null){
            if (headA.getVal() < headB.getVal()) {
                temp.setNext(headA);
                headA=headA.getNext();
                temp=temp.getNext();
            }else {
                temp.setNext(headB);
                headB=headB.getNext();
                temp=temp.getNext();
            }
        }
        if (headA != null){
            temp.setNext(headA);
        }else {
            temp.setNext(headB);
        }
        return newH.getNext();
    }

    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(23);
        mySingleList1.addLast(34);
        mySingleList1.addLast(45);
        mySingleList1.addLast(56);
        mySingleList1.addLast(67);
        mySingleList1.display();
        System.out.println("===================");
        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(13);
        mySingleList2.addLast(25);
        mySingleList2.addLast(35);
        mySingleList2.addLast(46);
        mySingleList2.addLast(146);
        mySingleList2.addLast(156);
        mySingleList2.display();
        System.out.println("===================");
        System.out.println("=======合并======");
        Test02 test02 = new Test02();
        MySingleList.ListNode after = test02.mergeTwoLists(mySingleList1.head,mySingleList2.head);

        mySingleList2.display2(after);
    }
}
